<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-10-25
 * Time: 下午4:43
 * To change this template use File | Settings | File Templates.
 */
class WEIXIN{
    private $db;
    private $table;
    private $per_page=20;
    private $offset;
    public $pager;
    function __construct($db){
        $this->db=$db;
        $this->table="weixin";
    }
    function lists($where=array()){//获取首页产品列表
        
        $result=$this->_lists_data($where);
        return $this->_list_result_cate($result);
    }
    
    function lists_where($where=array()){//按产品条件返回产品列表
       return $result=$this->_lists_data($where);
    }
    
    function detail($id){//获取详细信息
        $sql="select * from ".$this->table." where 1=1 and status=1 and wx_id='$id'";
        $result=$this->db->query_all($sql);
        return $result;
    }
    function iguesslike($keyword,$limit=7){//我猜你也喜欢
        if(empty($keyword)){//如果传入的关键字为空的话则默认显示排行靠前的
            $sql="select * from ".$this->table." where 1=1 and status=1 limit 0,$limit";
            $guessresult=$this->db->query_all($sql);
        }else{
            $guessresult=array();
            $keyword=explode(",",$keyword);
            if(!empty($keyword) and is_array($keyword)){
                foreach($keyword as $word){
                    $sql="select * from ".$this->table." where 1=1 and status=1 and keyword like '%$word%'";
                    $result=$this->db->query_all($sql);
                    $guessresult=array_merge($guessresult,$result);
                }
            }
        }
        return $guessresult;
    }
    function follow_top($limit=7){//关注排行
        $sql="select * from ".$this->table." where 1=1 and status=1 order by follow desc limit 0,$limit";
        $followresult=$this->db->query_all($sql);
        return $followresult;
    }
    
    ///以下为私有方法
    private function _page($where){
        $data_count=$this->_getTotal($where);
        //分页
        $records_per_page = $this->per_page;
        $total_pages = ceil($data_count/$records_per_page);
        
        $pager = new pager(array('total'=>$data_count,'perpage'=>$records_per_page));
        $pagenum = intval($_GET['pagenum'])<1?1:intval($_GET['pagenum']);
        $pagenum = ($pagenum>$total_pages)?$total_pages:$pagenum;
        $pagenum = ($pagenum<1)?1:$pagenum;
        $this->offset = ($pagenum-1)*$records_per_page;
        return $pager;
    }
    private function _getTotal($where){
        //得到总数
        $data_count_req = $this->db->query_first("SELECT count(wx_id) as t FROM ".$this->table." where 1=1 $where");
        
        $data_count = $data_count_req['t'];
        return $data_count;
    }
    private function _lists_data($where=array()){//获取产品列表
        $where_str=$this->_lists_where($where);
        $this->pager=$this->_page($where_str);
        $sql="select * from ".$this->table." where 1=1  and status=1 $where_str  order by sort desc,add_time desc LIMIT ".$this->offset.",".$this->per_page;
        $result=$this->db->query_all($sql);
        return $result;
    }
    private function _list_result_cate($result){//对返回的结果尽心分类处理
        $lists=array("top"=>array(),"spec"=>array(),"cate"=>array(),"life"=>array(),"other"=>array());
        foreach($result as $value){
            if(!is_array($value)){
                return $result;
            }else{
                if($value["top"]=="nomal"){//推荐
                    array_push($lists["top"],$value);
                    continue;
                }elseif($value["top"]=="spcial"){//特别推荐
                    array_push($lists["spec"],$value);
                    continue;
                }elseif($value["cate1"]=="娱乐休闲"){
                    array_push($lists["fashion"],$value);
                    continue;
                }elseif($value["cate1"]=="生活服务"){
                    array_push($lists["life"],$value);
                    continue;
                }elseif($value["cate1"]=="地方相关"){
                    array_push($lists["area"],$value);
                    continue;
                }else{
                    //print_r($lists["other"]);
                    array_push($lists["other"],$value);
                    continue;
                }
            }
        }
        return $lists;
    }
    
    private function _lists_where($where){//条件
        $where_str='';
        if($where['cate']){
            $where_str.=" and cate2='".$where['cate']."'";
        }elseif($where['area']){//地区模糊查找
            $where_str.=" and (province like '".$where['area']."%' or city like '".$where['area']."%' or area like '".$where['area']."%')";
        }elseif($where['p']){
            $where_str.=" and province='".$where['p']."'";
        }elseif($where['c']){
            $where_str.=" and city='".$where['c']."'";
        }elseif($where['a']){
            $where_str.=" and area='".$where['a']."'";
        }else{
            $where_str="";
        }
        return $where_str;
    }
}